﻿using ERP_SaaS_WinForm.生产计划_派单_车间.ProductPlan;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_SaaS_WinForm.生产计划_派单_车间
{
    public partial class ProductPlanMain : Form
    {
        private int pageIndex = 1;
        private int pageSize = 15;
        private int totalCount = 0;
        private int totalPage = 0;
        private void UpdatePagingButtons()
        {
            btnFirstPage.Enabled = pageIndex > 1;
            btnPreviousPage.Enabled = pageIndex > 1;
            btnNextPage.Enabled = pageIndex < totalPage;
            btnLastPage.Enabled = pageIndex < totalPage;
        }
        public ProductPlanMain()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 重置按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtProductCode.Text= string.Empty;
            txtProductName.Text= string.Empty;
            if(cboProductResource.Items.Count>0)
            {
                cboProductResource.SelectedIndex = 0;
            }
            if(cboProductState.Items.Count>0)
            {
                cboProductState.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnSearch_Click(object sender, EventArgs e)
        {
            string baseUrl = "http://localhost:5281/api/Plan/GetPlan";

            string planCode=txtProductCode.Text.Trim();
            string planName=txtProductName.Text.Trim();
            string resource = cboProductResource.SelectedValue?.ToString();
            string state=cboProductState.SelectedValue?.ToString();

            var queryParams = new List<string>();
            if (!string.IsNullOrEmpty(planCode)) queryParams.Add($"planCode={planCode}");
            if (!string.IsNullOrEmpty(planName)) queryParams.Add($"planName={planName}");
            if (!string.IsNullOrEmpty(resource)) queryParams.Add($"resource={resource}");
            if (!string.IsNullOrEmpty(state)) queryParams.Add($"state={state}");

            queryParams.Add($"pageIndex={pageIndex}");
            queryParams.Add($"pageSize={pageSize}");

            string fullUrl=$"{baseUrl}?{string.Join("&", queryParams)}";

            using (HttpClient client=new HttpClient())
            {
                HttpResponseMessage response=await client.GetAsync(fullUrl);
                response.EnsureSuccessStatusCode();
                string jsonResponse=await response.Content.ReadAsStringAsync();
                ApiResponse apiResponse = JsonConvert.DeserializeObject<ApiResponse>(jsonResponse);
                if (apiResponse.code==200)
                {
                    dgvPlanList.DataSource = apiResponse.data;

                    totalCount=apiResponse.totalCount;
                    totalPage=apiResponse.totalPage;

                    lblTotalCount.Text = $"共{totalCount}条";
                    lblTotalPage.Text = $"第{pageIndex}页/共{totalPage}页";

                    UpdatePagingButtons();

                }
                else
                {
                    MessageBox.Show($"查询失败{apiResponse.msg}");
                }
            }
        }

        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            if(pageIndex!=1)
            {
                pageIndex = 1;
                btnSearch_Click(sender, e);
            }
        }

        private void btnPreviousPage_Click(object sender, EventArgs e)
        {
            if (pageIndex>1)
            {
                pageIndex--;
                btnSearch_Click(sender, e);
            }
        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if(pageIndex<totalPage)
            {
                pageIndex++;
                btnSearch_Click(sender, e);
            }
        }

        private void btnLastPage_Click(object sender, EventArgs e)
        {
            if(pageIndex!=totalPage)
            {
                pageIndex=totalPage;
                btnSearch_Click(sender, e);
            }
        }

        private void btnGoPage_Click(object sender, EventArgs e)
        {
            if (int.TryParse(txtPageNumber.Text, out int pageNum) && pageNum >= 1 && pageNum <= totalPage)
            {
                pageIndex = pageNum;
                btnSearch_Click(sender, e);
            }
            else
            {
                MessageBox.Show("请输入有效的页码。");
            }
        }

        private  void ProductPlanMain_Load(object sender, EventArgs e)
        {
             btnSearch_Click(sender, e);
        }
    }
}
